package com.wlyy.bcwlw.common.export2excel.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.NotReadablePropertyException;
import org.springframework.util.Assert;

import com.wlyy.bcwlw.common.export2excel.entity.Export2ExcelColumn;
import com.wlyy.bcwlw.common.export2excel.entity.Export2ExcelRow;
/**
 * 
 * @ClassName. Export2ExcelConverterForRow
 * @Description. 导出excel转换成行
 * @author zhangyang
 * @date 2016年5月31日 上午8:59:09
 * @version V1.0
 */
public class Export2ExcelConverterForRow {

	
	public static Export2ExcelRow[] convertToRow(String[] colNames, Object[] objects){
		Assert.notEmpty(colNames);
		Assert.notEmpty(objects);
		BeanWrapper beanWrapper = null;
		List<Export2ExcelRow> rows = new ArrayList<Export2ExcelRow>();
		for(int i=0;i<objects.length;i++){
			beanWrapper = new BeanWrapperImpl(objects[i]);
			List<Export2ExcelColumn> columns = new ArrayList<Export2ExcelColumn>();
			Export2ExcelColumn column = null;
			for(int j=0;j<colNames.length;j++){
				try{
					column = new Export2ExcelColumn(colNames[j], beanWrapper
							.getPropertyValue(colNames[j]));
				} catch (NotReadablePropertyException e){
					column = new Export2ExcelColumn(colNames[j], null);
				}
				columns.add(column);
			}
			Export2ExcelRow export2ExcelRow = new Export2ExcelRow((Export2ExcelColumn[]) columns.toArray(new Export2ExcelColumn[0]));
			rows.add(export2ExcelRow);
		}
		return (Export2ExcelRow[])rows.toArray(new Export2ExcelRow[0]);
	}
}
